﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Domain.Models;
using System.Data;

namespace Domain.DAL
{
    public class CustomerDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public CustomerDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public Customer Get(long id)
        {
            Customer theCustomer = new Customer();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Customers_Get", id);
                if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theCustomer.ID = Convert.ToInt64(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["FullName"] != null && ds.Tables[0].Rows[0]["FullName"] != DBNull.Value)
                    {
                        theCustomer.FullName = ds.Tables[0].Rows[0]["FullName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"] != DBNull.Value)
                    {
                        theCustomer.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"] != DBNull.Value)
                    {
                        theCustomer.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["HomePhone"] != null && ds.Tables[0].Rows[0]["HomePhone"] != DBNull.Value)
                    {
                        theCustomer.HomePhone = ds.Tables[0].Rows[0]["HomePhone"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theCustomer.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theCustomer;
        }

        #endregion

        #region Public Static Methods

        public static CustomerDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<CustomerDAO>();
        }

        #endregion
    }
}
